﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QQZiFramework
{
    /// <summary>
    /// Json日期格式化处理（不包括时间）
    /// </summary>
    public class UnixDateConverter : DateTimeConverterBase
    {

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.Value is DateTime) return reader.Value;
            if (reader.TokenType != JsonToken.String && reader.Value.ToString().Length < 10)
            {
                throw new Exception(String.Format("日期格式错误, {0}.", reader.Value));
            }
            return Convert.ToDateTime((string)reader.Value);
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {

            if (value is DateTime)
            {
                writer.WriteValue(((DateTime)value).ToString("yyyy-MM-dd"));
            }
            else
            {
                throw new Exception("时间格式错误.2");
            }

        }
    }
    /// <summary>
    /// Json日期格式化处理（包括时间）
    /// </summary>
    public class UnixTimeConverter : DateTimeConverterBase
    {
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.Value == null) return DateTime.MinValue;
            if (reader.TokenType != JsonToken.String && reader.Value.ToString().Length < 10)
            {
                throw new Exception(String.Format("日期格式错误, {0}.", reader.Value));
            }
            if (reader.Value is DateTime) return reader.Value;
            else if (reader.Value is Int64 || reader.Value is Double)
            {
                Int64 d = Convert.ToInt64(reader.Value);
                //时间戳                     
                if (d > 99999999999)
                {
                    //毫秒
                    return Utility.ToDateTimeFromMillise(d);
                }
                else
                {
                    //秒
                    return Utility.ToDateTime(d);
                }
            }
            else
            {
                string str = (string)reader.Value;
                if (Utility.IsNumeric(str))
                {
                    //时间戳                     
                    if (str.Length > 10)
                    {
                        //毫秒
                        return Utility.ToDateTimeFromMillise(Convert.ToInt64(str));
                    }
                    else
                    {
                        //秒
                        return Utility.ToDateTime(Convert.ToInt64(str));
                    }
                }
                return Convert.ToDateTime(str);
            }
        }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {

            if (value is DateTime)
            {
                writer.WriteValue(((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"));
            }
            else
            {
                throw new Exception("时间格式错误.不是DateTime类型");
            }

        }
    }
}
